package com.ltmall.api.dao.impl;

import com.ltmall.api.bean.DirectorPrivilegePackage;
import com.ltmall.api.dao.IDirectorPrivilegePackageDAO;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import java.util.ArrayList;
import java.util.List;

import static com.ltmall.api.util.HibernateUtil.closeSession;
import static com.ltmall.api.util.HibernateUtil.getSession;

public class DirectorPrivilegePackageDAO extends HibernateDaoSupport implements IDirectorPrivilegePackageDAO {

	@Override
	public DirectorPrivilegePackage getDirectorPrivilegePackageById(int id) {
		Session session = getSession();
		try {
			return (DirectorPrivilegePackage) session.get(DirectorPrivilegePackage.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			//releaseSession(session);
			closeSession();
		}
	}

	@Override
	public List<DirectorPrivilegePackage> listAllDirectorPrivilegePackage() {
		final String SQL = "select * from flavor_director_privilege_package";
		Session session = getSession();
		Query query = session.createSQLQuery(SQL).addEntity(DirectorPrivilegePackage.class);
		ArrayList<DirectorPrivilegePackage> list = new ArrayList<DirectorPrivilegePackage>();
		for(Object obj : query.list()) {
			list.add((DirectorPrivilegePackage) obj);
		}		
		return list;
	}

}
